/*
* EnergyBar
*
* Copyright (C) Connect Link 2013-
*/


package com.connectlinkcorp.common.ui.energybar;

import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.Toast;



public class EnergyBar extends LinearLayout {

	public static final int HORIZONTAL = 1;
	public static final int VERTICAL = 0;
	private Context context;
	private int duration; // in milisecond
	private int orientation;
	private int positionX;
	private int positionY;
	private int width;
	private int height;
	private LinearLayout liLayoutMaxProgress;
	private LinearLayout liLayoutProgress;
	private Handler handler ;
	private int timeUpdate;
	private int lengthStep;
	private int currentLong;
	
	public EnergyBar(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		liLayoutMaxProgress = new LinearLayout(context);
		liLayoutProgress = new LinearLayout(context);
		this.addView(liLayoutMaxProgress);
		liLayoutMaxProgress.addView(liLayoutProgress);
		this.setBackgroundColor(Color.TRANSPARENT);
		liLayoutMaxProgress.setBackgroundColor(Color.RED);
		liLayoutProgress.setBackgroundColor(Color.WHITE);
	}
	
	public EnergyBar (Context context, AttributeSet  attr) {
		super(context, attr);
		// TODO Auto-generated constructor stub
		this.context= context;
		liLayoutMaxProgress = new LinearLayout(context);
		liLayoutProgress = new LinearLayout(context);
		this.addView(liLayoutMaxProgress);
		liLayoutMaxProgress.addView(liLayoutProgress);
		this.setBackgroundColor(Color.TRANSPARENT);
		liLayoutMaxProgress.setBackgroundColor(Color.RED);
		liLayoutProgress.setBackgroundColor(Color.WHITE);
	}
	
	
	public EnergyBar (int barOrientation, int durationInMilisecond, int X, int Y, int mWidth, int mHeight, Context context){
		super(context);
		this.context = context;
		orientation = barOrientation;
		duration = durationInMilisecond;
		positionX = X;
		positionY = Y;
		width = mWidth;
		height = mHeight;
		timeUpdate = 100;
		currentLong = 0;
		liLayoutMaxProgress = new LinearLayout(context);
		liLayoutProgress = new LinearLayout(context);
		this.addView(liLayoutMaxProgress);
		liLayoutMaxProgress.addView(liLayoutProgress);
		this.setBackgroundColor(Color.TRANSPARENT);
		liLayoutMaxProgress.setBackgroundColor(Color.RED);
		liLayoutProgress.setBackgroundColor(Color.WHITE);
	}
	
	public void setUpParam (int barOrientation, int durationInMilisecond, int X, int Y, int mWidth, int mHeight, Context context) {
		this.context = context;
		orientation = barOrientation;
		duration = durationInMilisecond;
		positionX = X;
		positionY = Y;
		width = mWidth;
		height = mHeight;
		timeUpdate = 100;
		currentLong = 0;
	}
	
	public void setBackgroundEnergyBar (int color) {
		liLayoutMaxProgress.setBackgroundColor(color);
	}
	
	public void setBacgroundProgressBar (int color) {
		liLayoutProgress.setBackgroundColor(color);
	}
	
	public void show () {
		handler = new Handler();
		if (orientation == VERTICAL) {
			LayoutParams paramFullProgress = new LayoutParams(width, height);
			paramFullProgress.setMargins(positionX, positionY, 0, 0);
			liLayoutMaxProgress.setLayoutParams(paramFullProgress);
			
			LayoutParams paramProgress = new LayoutParams (width, currentLong);
			paramProgress.setMargins(0, 0, 0, 0);
			liLayoutProgress.setLayoutParams(paramProgress);
		} else if (orientation == HORIZONTAL) {
			LayoutParams paramFullProgress = new LayoutParams(width, height);
			paramFullProgress.setMargins(positionX, positionY, 0, 0);
			liLayoutMaxProgress.setLayoutParams(paramFullProgress);
			
			LayoutParams paramProgress = new LayoutParams (currentLong, height);
			paramProgress.setMargins(0, 0, 0, 0);
			liLayoutProgress.setLayoutParams(paramProgress);
		}
		lengthStep = 0;
		handler.postDelayed(runnable, timeUpdate);
	}
	
	public void start () {
		if (orientation == VERTICAL) {
			lengthStep = (int)(height / (int)(duration/timeUpdate));
			Toast.makeText(context, String.valueOf(100 -(currentLong*100)/height) + "%", Toast.LENGTH_SHORT).show();
		} else if (orientation == HORIZONTAL){
			lengthStep = (int)(width / (int)(duration/timeUpdate));
			Toast.makeText(context, String.valueOf(100 - (currentLong*100)/width) + "%", Toast.LENGTH_SHORT).show();
		}
	}
	
	public void stop () {
		lengthStep = 0;
		if (orientation == VERTICAL) {
			Toast.makeText(context, String.valueOf(100 - (currentLong*100)/height) + "%", Toast.LENGTH_SHORT).show();
		} else if (orientation == HORIZONTAL){
			Toast.makeText(context, String.valueOf(100 - (currentLong*100)/width) + "%", Toast.LENGTH_SHORT).show();
		}
	}

	public int getDuration() {
		return duration;
	}

	public void setDuration(int duration) {
		this.duration = duration;
	}

	public int getOrientation() {
		return orientation;
	}

	public void setOrientation(int orientation) {
		this.orientation = orientation;
	}

	public int getPositionX() {
		return positionX;
	}

	public void setPositionX(int positionX) {
		this.positionX = positionX;
	}

	public int getPositionY() {
		return positionY;
	}

	public void setPositionY(int positionY) {
		this.positionY = positionY;
	}

	public int getWidthOfBar() {
		return width;
	}

	public void setWidthOfBar(int width) {
		this.width = width;
	}

	public int getHeightOfBar() {
		return height;
	}

	public void setHeightOfBar(int height) {
		this.height = height;
	}

	public int getCurrentPercent() {
		if (orientation == HORIZONTAL) {
			return (currentLong*100)/width;
		} else  {
			return (currentLong*100)/height;
		}
	}
	
	Runnable runnable = new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
			currentLong = currentLong + lengthStep;
			Log.d("current Long", String.valueOf(currentLong));
			if (orientation == VERTICAL){
				LayoutParams param = new LayoutParams (width, currentLong);
				param.setMargins(0, 0, 0, 0);
				liLayoutProgress.setLayoutParams(param);
				if(currentLong == height) {
					handler.removeCallbacks(runnable);
					Toast.makeText(context, "0" + "%", Toast.LENGTH_SHORT).show();
				} else {
					handler.postDelayed(runnable, timeUpdate);
				}
			} else if (orientation == HORIZONTAL){
				LayoutParams param = new LayoutParams (currentLong, height);
				param.setMargins(0, 0, 0, 0);
				liLayoutProgress.setLayoutParams(param);
				if(currentLong == width) {
					handler.removeCallbacks(runnable);
					Toast.makeText(context, "0" + "%", Toast.LENGTH_SHORT).show();
				} else {
					handler.postDelayed(runnable, timeUpdate);
				}
			}
		}
	};

	
	
}
